home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 18 gdiplus / gdidemo / typography.vb < prev   
Encoding:
Text File  |  2001-10-18  |  12.5 KB  |  321 lines

  1. Public Class Typography
  2.     Inherits System.Windows.Forms.Form
  3.  
  4. #Region " Windows Form Designer generated code "
  5.  
  6.     Public Sub New()
  7.         MyBase.New()
  8.  
  9.         'This call is required by the Windows Form Designer.
  10.         InitializeComponent()
  11.  
  12.         'Add any initialization after the InitializeComponent() call
  13.  
  14.     End Sub
  15.  
  16.     'Form overrides dispose to clean up the component list.
  17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  18.         If disposing Then
  19.             If Not (components Is Nothing) Then
  20.                 components.Dispose()
  21.             End If
  22.         End If
  23.         MyBase.Dispose(disposing)
  24.     End Sub
  25.     Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
  26.     Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
  27.     Friend WithEvents mnuEnumerateFonts As System.Windows.Forms.MenuItem
  28.     Friend WithEvents mnuFontStyles As System.Windows.Forms.MenuItem
  29.     Friend WithEvents mnuTextureBrush As System.Windows.Forms.MenuItem
  30.     Friend WithEvents mnuAlignedText As System.Windows.Forms.MenuItem
  31.     Friend WithEvents mnuVerticalText As System.Windows.Forms.MenuItem
  32.     Friend WithEvents mnuTabStops As System.Windows.Forms.MenuItem
  33.     Friend WithEvents mnuAntialiasing As System.Windows.Forms.MenuItem
  34.  
  35.     'Required by the Windows Form Designer
  36.     Private components As System.ComponentModel.Container
  37.  
  38.     'NOTE: The following procedure is required by the Windows Form Designer
  39.     'It can be modified using the Windows Form Designer.  
  40.     'Do not modify it using the code editor.
  41.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  42.         Me.mnuFontStyles = New System.Windows.Forms.MenuItem()
  43.         Me.mnuTabStops = New System.Windows.Forms.MenuItem()
  44.         Me.mnuVerticalText = New System.Windows.Forms.MenuItem()
  45.         Me.mnuEnumerateFonts = New System.Windows.Forms.MenuItem()
  46.         Me.mnuTextureBrush = New System.Windows.Forms.MenuItem()
  47.         Me.mnuAlignedText = New System.Windows.Forms.MenuItem()
  48.         Me.MainMenu1 = New System.Windows.Forms.MainMenu()
  49.         Me.MenuItem1 = New System.Windows.Forms.MenuItem()
  50.         Me.mnuAntialiasing = New System.Windows.Forms.MenuItem()
  51.         '
  52.         'mnuFontStyles
  53.         '
  54.         Me.mnuFontStyles.Index = 1
  55.         Me.mnuFontStyles.Text = "Font styles"
  56.         '
  57.         'mnuTabStops
  58.         '
  59.         Me.mnuTabStops.Index = 5
  60.         Me.mnuTabStops.Text = "Tab Stops"
  61.         '
  62.         'mnuVerticalText
  63.         '
  64.         Me.mnuVerticalText.Index = 4
  65.         Me.mnuVerticalText.Text = "Vertical Text"
  66.         '
  67.         'mnuEnumerateFonts
  68.         '
  69.         Me.mnuEnumerateFonts.Index = 0
  70.         Me.mnuEnumerateFonts.Text = "Enumerate Font Families"
  71.         '
  72.         'mnuTextureBrush
  73.         '
  74.         Me.mnuTextureBrush.Index = 2
  75.         Me.mnuTextureBrush.Text = "Textured Brushes"
  76.         '
  77.         'mnuAlignedText
  78.         '
  79.         Me.mnuAlignedText.Index = 3
  80.         Me.mnuAlignedText.Text = "Aligned Text"
  81.         '
  82.         'MainMenu1
  83.         '
  84.         Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})
  85.         '
  86.         'MenuItem1
  87.         '
  88.         Me.MenuItem1.Index = 0
  89.         Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuEnumerateFonts, Me.mnuFontStyles, Me.mnuTextureBrush, Me.mnuAlignedText, Me.mnuVerticalText, Me.mnuTabStops, Me.mnuAntialiasing})
  90.         Me.MenuItem1.Text = "Examples"
  91.         '
  92.         'mnuAntialiasing
  93.         '
  94.         Me.mnuAntialiasing.Index = 6
  95.         Me.mnuAntialiasing.Text = "Anti-aliasing"
  96.         '
  97.         'Typography
  98.         '
  99.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
  100.         Me.ClientSize = New System.Drawing.Size(680, 353)
  101.         Me.Menu = Me.MainMenu1
  102.         Me.Name = "Typography"
  103.         Me.Text = "Typography"
  104.  
  105.     End Sub
  106.  
  107. #End Region
  108.  
  109.     Private Sub mnuEnumerateFonts_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuEnumerateFonts.Click
  110.         ' Draw all font names on form
  111.         Dim gr As Graphics = Me.CreateGraphics
  112.         gr.Clear(Color.White)
  113.  
  114.         ' Get the collection of installed Fonts.
  115.         Dim fonts As New System.Drawing.Text.InstalledFontCollection()
  116.         ' Get an array with all installed font families.
  117.         Dim fontFamilies() As FontFamily = fonts.Families
  118.  
  119.         ' Uncomment next statement to see only fonts for video device.
  120.         ' fontFamilies = FontFamily.GetFamilies(gr)
  121.  
  122.         ' Create a comma-delimited list of font families names.
  123.         Dim list As String
  124.         Dim font As FontFamily
  125.         For Each font In fontFamilies
  126.             If list <> "" Then list &= ", "
  127.             list &= font.Name
  128.         Next
  129.  
  130.  
  131.         ' use a font for printing.
  132.         Dim fnt As New Font("Arial", 10, FontStyle.Regular)
  133.         ' Keep output inside the form client area.
  134.         Dim rectF As New RectangleF(0, 0, Me.ClientRectangle.Width, Me.ClientRectangle.Height)
  135.         gr.DrawString(list, fnt, Brushes.Black, rectF)
  136.  
  137.         fnt.Dispose()
  138.         gr.Dispose()
  139.  
  140.     End Sub
  141.  
  142.     Private Sub mnuFontStyles_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuFontStyles.Click
  143.         ' let the menu disappear.
  144.         Threading.Thread.Sleep(500)
  145.  
  146.         ' Draw all font names on form
  147.         Dim gr As Graphics = Me.CreateGraphics
  148.         gr.Clear(Color.White)
  149.  
  150.         ' Create a font from a family and a size in points.
  151.         Dim font1 As New Font("Arial", 12, GraphicsUnit.Pixel)
  152.         ' Create a font from a family, a size, and a style.
  153.         Dim font2 As New Font("Arial", 14, FontStyle.Bold)
  154.         ' Create a font from a family, a size, and two combined styles.
  155.         Dim font3 As New Font("Arial", 16, FontStyle.Italic Or FontStyle.Underline)
  156.         ' Create a font from a family and a size in another unit.
  157.         Dim font4 As New Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Millimeter)
  158.  
  159.         ' Create a font family
  160.         Dim fontFam As New FontFamily("Courier New")
  161.         Dim font5 As New Font(fontFam, 18, FontStyle.Italic)
  162.  
  163.         gr.DrawString("Arial 12 Regular", font1, Brushes.Black, 20, 20)
  164.         gr.DrawString("Arial 14 Bold", font2, Brushes.Black, 20, 60)
  165.         gr.DrawString("Arial 16 Italic & Underline", font3, Brushes.Black, 20, 100)
  166.         gr.DrawString("Arial 10 millimeters", font4, Brushes.Black, 20, 140)
  167.         gr.DrawString("Courier 18 Italic", font5, Brushes.Black, New PointF(20, 200))
  168.  
  169.         ' Redraw the same font output with a more compact line spacing.
  170.         Dim y As Integer = 250
  171.         gr.DrawString("Arial 12 Regular", font1, Brushes.Black, 20, y)
  172.         y += font1.GetHeight(gr)
  173.         gr.DrawString("Arial 14 Bold", font2, Brushes.Black, 20, y)
  174.         y += font2.GetHeight(gr)
  175.         gr.DrawString("Arial 16 Italic & Underline", font3, Brushes.Black, 20, y)
  176.         y += font3.GetHeight(gr)
  177.         gr.DrawString("Arial 10 millimeters", font4, Brushes.Black, 20, y)
  178.         y += font4.GetHeight(gr)
  179.         gr.DrawString("Courier 18 Italic", font5, Brushes.Black, New PointF(20, y))
  180.         y += font5.GetHeight(gr)
  181.  
  182.         font1.Dispose()
  183.         font2.Dispose()
  184.         font3.Dispose()
  185.         font4.Dispose()
  186.         font5.Dispose()
  187.         gr.Dispose()
  188.     End Sub
  189.  
  190.     Private Sub mnuTextureBrush_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuTextureBrush.Click
  191.         ' let the menu disappear.
  192.         Threading.Thread.Sleep(500)
  193.  
  194.         ' Draw all font names on form
  195.         Dim gr As Graphics = Me.CreateGraphics
  196.         gr.Clear(Color.White)
  197.         ' Create a textured brush.
  198.         Dim br As New TextureBrush(Image.FromFile("greenstone.bmp"))
  199.         ' create a large font.
  200.         Dim fnt As New Font("Arial", 50, FontStyle.Bold, GraphicsUnit.Millimeter)
  201.         ' Paint a string with textured pattern.
  202.         gr.DrawString("Textured", fnt, br, 20, 20)
  203.         gr.DrawString("Text", fnt, br, 20, 200)
  204.  
  205.         fnt.Dispose()
  206.         br.Dispose()
  207.         gr.Dispose()
  208.     End Sub
  209.  
  210.     Private Sub mnuAlignedText_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuAlignedText.Click
  211.         ' let the menu disappear.
  212.         Threading.Thread.Sleep(500)
  213.  
  214.         ' Draw all font names on form
  215.         Dim gr As Graphics = Me.CreateGraphics
  216.         gr.Clear(Color.White)
  217.  
  218.         Dim msg As String = "This is a long string whose purpose is to show how" _
  219.             & " you can format a message inside a rectangle."
  220.  
  221.         Dim fnt As New Font("Arial", 12)
  222.         Dim rectF As RectangleF
  223.  
  224.         ' Draw the text inside the rectangle.
  225.         rectF = New RectangleF(20, 20, 140, 160)
  226.         gr.DrawString(msg, fnt, Brushes.Black, rectF)
  227.         ' Also display the bounding rectangle.
  228.         gr.DrawRectangle(Pens.Red, 20, 20, 140, 160)
  229.  
  230.         ' Draw the text again, but align it to the right.
  231.         Dim strFormat As New StringFormat()
  232.         strFormat.Alignment = StringAlignment.Far
  233.         ' Draw the text inside the rectangle, with the format string.
  234.         rectF = New RectangleF(220, 20, 140, 160)
  235.         gr.DrawString(msg, fnt, Brushes.Black, rectF, strFormat)
  236.         ' Also display the bounding rectangle.
  237.         gr.DrawRectangle(Pens.Red, 220, 20, 140, 160)
  238.  
  239.         ' Draw the text again, but center it horizontally AND vertically.
  240.         strFormat.Alignment = StringAlignment.Center
  241.         strFormat.LineAlignment = StringAlignment.Center
  242.         ' Draw the text inside the rectangle, with the format string.
  243.         rectF = New RectangleF(420, 20, 140, 160)
  244.         gr.DrawString(msg, fnt, Brushes.Black, rectF, strFormat)
  245.         ' Also display the bounding rectangle.
  246.         gr.DrawRectangle(Pens.Red, 420, 20, 140, 160)
  247.  
  248.         fnt.Dispose()
  249.         gr.Dispose()
  250.     End Sub
  251.  
  252.     Private Sub mnuVerticalText_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuVerticalText.Click
  253.         ' let the menu disappear.
  254.         Threading.Thread.Sleep(500)
  255.  
  256.         ' Draw all font names on form
  257.         Dim gr As Graphics = Me.CreateGraphics
  258.         gr.Clear(Color.White)
  259.  
  260.         Dim msg As String = "A vertical string"
  261.         Dim fnt As New Font("Arial", 12)
  262.         Dim strFormat As New StringFormat()
  263.         strFormat.FormatFlags = StringFormatFlags.DirectionVertical
  264.         gr.DrawString(msg, fnt, Brushes.Black, 20, 20, strFormat)
  265.  
  266.         fnt.Dispose()
  267.         gr.Dispose()
  268.     End Sub
  269.  
  270.     Private Sub mnuTabStops_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuTabStops.Click
  271.         ' let the menu disappear.
  272.         Threading.Thread.Sleep(500)
  273.  
  274.         ' Draw all font names on form
  275.         Dim gr As Graphics = Me.CreateGraphics
  276.         gr.Clear(Color.White)
  277.  
  278.         ' Prepare a message with tabs and carriage returns.
  279.         Dim msg As String = String.Format("{0}Column 1{0}Column 2{0}Column 3{1}" _
  280.             & "Row 1{0}Cell (1,1){0}Cell (1,2){0}Cell (1,3){1}" _
  281.             & "Row 2{0}Cell (2,1){0}Cell (2,2){0}Cell (3,3){1}", _
  282.             ControlChars.Tab, ControlChars.CrLf)
  283.         Dim fnt As New Font("Arial", 12)
  284.  
  285.         Dim strFormat As New StringFormat()
  286.         ' Set the tab stops.
  287.         Dim tabStops() As Single = {80, 140, 200}
  288.         strFormat.SetTabStops(0, tabStops)
  289.         ' Draw the text with specified tab stops.
  290.         gr.DrawString(msg, fnt, Brushes.Black, 20, 20, strFormat)
  291.  
  292.         fnt.Dispose()
  293.         gr.Dispose()
  294.     End Sub
  295.  
  296.     Private Sub mnuAntialiasing_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuAntialiasing.Click
  297.         ' let the menu disappear.
  298.         Threading.Thread.Sleep(500)
  299.  
  300.         ' Draw all font names on form
  301.         Dim gr As Graphics = Me.CreateGraphics
  302.         gr.Clear(Color.White)
  303.  
  304.         ' Draw a first ellipse in regular mode.
  305.         gr.DrawEllipse(Pens.Black, 20, 20, 100, 60)
  306.         ' Draw a second ellipse in anti-aliasing mode.
  307.         gr.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias
  308.         gr.DrawEllipse(Pens.Black, 140, 20, 100, 60)
  309.  
  310.         Dim fnt As New Font("Arial", 14)
  311.         gr.DrawString("Regular Text", fnt, Brushes.Black, 20, 200)
  312.         gr.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
  313.         gr.DrawString("Standard Anti-aliasing", fnt, Brushes.Black, 20, 260)
  314.         gr.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
  315.         gr.DrawString("ClearType Anti-aliasing", fnt, Brushes.Black, 20, 320)
  316.  
  317.         fnt.Dispose()
  318.         gr.Dispose()
  319.     End Sub
  320. End Class
  321.